package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    /**
     * 添加或修改公寓信息
     *
     * @param apartmentSubmitVo
     */
    @Override
    public void saveOrUpdateAppartInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //根据传过来的参数先判断是修改还是添加，如果传过来的参数有id就是添加，没有id就是修改
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        //添加
        this.saveOrUpdate(apartmentSubmitVo);
        //获取公寓ID
        Long apartmentId = apartmentSubmitVo.getId();
        //如果isUpdate！=null就是修改
        if(isUpdate){
            //删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper=new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId,apartmentId);
            graphInfoService.remove(graphInfoQueryWrapper);
            //删除标签信息
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper=new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentId);
            apartmentLabelService.remove(labelQueryWrapper);
            //删除配套信息
            LambdaQueryWrapper<ApartmentFacility> faciltyQueryWrapper=new LambdaQueryWrapper<>();
            faciltyQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
            apartmentFacilityService.remove(faciltyQueryWrapper);
            //删除杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueQueryWrapper=new LambdaQueryWrapper<>();
            feeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentId);
            apartmentFeeValueService.remove(feeValueQueryWrapper);
        }
        //修改图片信息
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getGraphVoList())){
            List<GraphInfo> graphInfoList=new ArrayList<>();
            for (GraphVo graphVo : apartmentSubmitVo.getGraphVoList()) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //修改标签信息
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getLabelIds())){
            List<ApartmentLabel> apartmentLabelList=new ArrayList<>();
            for (Long labelId : apartmentSubmitVo.getLabelIds()) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelId).build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        //修改配套信息
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getFacilityInfoIds())){
            List<ApartmentFacility> apartmentFacilityList=new ArrayList<>();
            for (Long facilityInfoId : apartmentSubmitVo.getFacilityInfoIds()) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }
        //修改杂费信息
        if(!CollectionUtils.isEmpty(apartmentSubmitVo.getFeeValueIds())){
            List<ApartmentFeeValue> apartmentFeeValueList=new ArrayList<>();
            for (Long feeValueId : apartmentSubmitVo.getFeeValueIds()) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentId).build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

    }

    /**
     * 根据queryVo查询公寓的分页列表信息
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<ApartmentItemVo> selectApartmentPageByQueryVo(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.selectApartmentPageByQueryVo(page,queryVo);
    }

    /**
     * 根据id获取公寓详细信息
     * @param apartmentId
     * @return
     */
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long apartmentId) {
        //先获取公寓的基本信息
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        //查询图片信息
        List<GraphVo> graphVoList=graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,apartmentId);
        //查询配套信息
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartmentId(apartmentId);
        //查询标签信息
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByApartmentId(apartmentId);
        //查询杂费信息
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectListByApartmentId(apartmentId);
        //创建ApartmentDetailVo对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //组合查询到的信息
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    /**
     * 根据ID删除公寓信息
     * @param id
     */
    @Override
    public void removeApartmentById(Long id) {
        //先判断公寓里是否有房间
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);

        //5.删除ApartmentInfo
        this.removeById(id);
    }
}




